<template>
  <!-- 英雄联盟自动获取授权 -->
  <el-dialog :title="'扫码授权'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="300px" append-to-body
    @close="close">
    <div class="form-box">
      <div class="demo-ruleForm">
        <img :src="qrCode" alt="" style="width: 200px;height: 200px;">
        <div class="center">
          {{ msg||'请扫码授权' }}
        </div>
        <div><el-button @click="getToken">我已授权</el-button></div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import autoApi from "@/api/autoProduction/information.js"
export default {
  name: "addFina",
  data() {
    return {
      dialogVisible: false,
      loading: false,
      msg: '',
      qrCode: '',
      uuid: '',
      timer: null,
      type: 'CF',
      params: {},
      tencentId: ''
    };
  },
  created() {

  },
  beforeDestroy() {
    window.clearInterval(this.timer)
  },
  methods: {
    // 打开弹窗
    open(data) {
      this.params.gameId = data.gameId
      this.params.type = data.type

      // this.type = type
      this.dialogVisible = true
      this.getQrCode()
    },
    close() {
      window.clearInterval(this.timer)
    },
    getQrCode() {
      autoApi.getCFQRcode(this.params).then(res => {
        console.log("%c Line:52 🍻 res", "color:#ffdd4d", res);
        if (res.code == 200) {

          this.qrCode = res.data.qrcode
          this.params.uuid = res.data.uuid
          this.getTokenInterval()
        }
      })
    },
    getToken() {
      autoApi.getXLLoginFindRe(this.params).then(res => {
        if (res.code == 200) {
          this.msg = res.data.msg
          if(res.data.msg=='二维码未失效。'){
            this.msg = '请扫码授权'
          }
          if (res.data.code == 200) {
            // 认证成功  请求数据
            window.clearInterval(this.timer)
            this.timer = null
            this.tencentId = res.data.tencentInfoId
            this.getGameInfos()
          }else if(res.data.code == 400){
            this.msg = ''
            this.getQrCode()
          }
        }else{
          this.getQrCode()
        }
      }).catch(err => {
        if (err.code == 500) {
          
        }
      })
    },
    getGameInfos() {
      window.clearInterval(this.timer)
      let obj = {
        tencentId: this.tencentId,
        gameId: this.params.gameId
      }
      autoApi.getXLGameInfo(obj).then(res => {
        console.log("%c Line:96 🍰 获取到的CF游戏账号详情res", "color:#42b983", res);
        if (res.code == 200) {
          this.$emit('success', {...res.data,gameId:this.params.gameId})
          this.dialogVisible = false
        }
      })
    },

    getTokenInterval() {
      window.clearInterval(this.timer)
      this.timer = window.setInterval(() => {
        this.getToken()
      }, 5000)
    }

  }
};
</script>
<style scoped lang="css">
.demo-ruleForm {
  text-align: center;
}
</style>
